#include "CCmdInnerCheckName.h"
#include "../mainctrl.h"
#include "../cmd/cmdapi.h"
#include "commmsg.h"
#include "../cmdfront/cmduserinfo.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_CHECK_NAME, CCmdInnerCheckName);

int CCmdInnerCheckName::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_ERROR(("inner cmd CCmdInnerCheckName get data"));
        return AnsClients(RET_SYS_ERROR);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return AnsClients(RET_SYS_ERROR);
    }

    if (strlen(_stPara.GetValue("platform")) <= 0)
    {
        CT_ERROR((""));
        return AnsClients(RET_SYS_ERROR);
    }

    if (pstMainCtrl->IsTencentPlatform(_stPara.GetValue("platform")))
    {
        if (strlen(_stPara.GetValue("openid")) <= 0 ||
            strlen(_stPara.GetValue("openkey")) <= 0 ||
            strlen(_stPara.GetValue("pf")) <= 0 ||
            strlen(_stPara.GetValue("seqid")) <= 0 ||
            strlen(_stPara.GetValue("pfkey")) <= 0)
        {
            CT_ERROR((""));
            return AnsClients(RET_SYS_ERROR);
        }
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdInnerCheckName::LogicDo(int iRet)
{
    if (iRet != RET_OK)
    {
        CT_ERROR(("CCmdInnerCheckName error , ret %d", iRet));
        return AnsClients(RET_SYS_ERROR);
    }

    if (!_bHasCheckName)
    {
        _bHasCheckName = true;

        CTSizeString<ROLENAME_LEN> szRoleName;
        szRoleName.Set("%s", CTStringUtils::UrlDecode(_stPara.GetValue("rolename")));

        if (szRoleName.GetDataLen() <= 0)
        {
            return AnsClients(ERROR_WRONG_NAME);
        }

        iRet = CCmdInitUserInfo::CheckNewRoleName(szRoleName);
        if (iRet != RET_OK)
        {
            return AnsClients(iRet);
        }

        if (!pstMainCtrl->IsTencentPlatform(_stPara.GetValue("platform")))
        {
            return AnsClients(RET_OK);
        }

        if (strlen(_stPara.GetValue("openid")) <= 0 ||
            strlen(_stPara.GetValue("openkey")) <= 0  ||
            strlen(_stPara.GetValue("pf")) <= 0  ||
            strlen(_stPara.GetValue("pfkey")) <= 0 )
        {
            CT_ERROR(("TencentApiKeyValue Error!"));
            return AnsClients(RET_REQ_NOT_COMPLETE);
        }

        struct in_addr ip;
        ip.s_addr = stEnpHead.uiRemoteIP;

        CTLib::CTKeyValue stTencentApiKeyValue;

        stTencentApiKeyValue.SetValue("openid", _stPara.GetValue("openid"));
        stTencentApiKeyValue.SetValue("openkey", _stPara.GetValue("openkey"));
        stTencentApiKeyValue.SetValue("pf", _stPara.GetValue("pf"));
        stTencentApiKeyValue.SetValue("appid", CONF->AppID());
        stTencentApiKeyValue.SetValue("pfkey", _stPara.GetValue("pfkey"));
        stTencentApiKeyValue.SetValue("app_contract_id", "");
        stTencentApiKeyValue.SetValue("userip",inet_ntoa(ip));

        CCmdApi* pstCmd = (CCmdApi*)CT_CMDFACTORY->CreateCmd(ID_MAINAPI_TAPI);
        if (!pstCmd)
        {
            CT_ERROR(("create cmd CCmdApi error"));
            return AnsClients(RET_SYS_ERROR);
        }

        pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
        pstCmd->stReq._ucReqType = CReqApi::API_TENCENT;
        pstCmd->stReq._szTApiName.Set("/v3/csec/word_filter");
        pstCmd->stReq._stTApiKeyValue = stTencentApiKeyValue;
        pstCmd->stReq._stTApiKeyValue.SetValue("content", szRoleName());

        uint64 u64MsgID = CT_UINT64_MAKE(pstMainCtrl->m_stNowTime.tv_sec,
            pstMainCtrl->m_stNowTime.tv_usec);

        pstCmd->stReq._stTApiKeyValue.SetValue("msgid",
            CTStringUtils::Uint64ToString(u64MsgID, 16));

        CT_TRACE(("_bHasCheckName"));
        RegisterChild(pstCmd, true);

        return pstCmd->Do(NULL);
    }
    else
    {
        if (m_pstChild && ID_MAINAPI_TAPI == m_pstChild->GetCmdID())
        {
            CCmdApi* pstCmd = (CCmdApi*)m_pstChild;
            if (pstCmd->stReq._szTApiName == "/v3/csec/word_filter")
            {
                if (atoi(pstCmd->stAns._stTApiKeyValue.GetValue("is_dirty")) != 0)
                {
                    return AnsClients(ERROR_HAS_SEN_WORD);
                }
            }
        }
    }

    return AnsClients(RET_OK);
}

int CCmdInnerCheckName::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();
    CTSizeString<64> szTmp;
    CTSizeString<1024> szTmp2;

    if (iRet)
    {
        szTmp.Set("%d", iRet);
        szTmp2.Set("HTTP/1.1 200 OK\r\nContent-length:%d\r\n\r\n%s\r\n",
            (szTmp.GetDataLen() + 2), szTmp());
    }
    else
    {
        szTmp2.Set("HTTP/1.1 200 OK\r\nContent-length:3\r\n\r\n0\r\n");
    }
    
    memcpy(pstMainCtrl->m_stEncodeBuf.GetFreeBuf(), szTmp2(),
        szTmp2.GetDataLen());

    pstMainCtrl->m_stEncodeBuf.Append(szTmp2.GetDataLen());

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

